package com.videoadmin.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.videoadmin.factory.DAOFactory;
import com.videoadmin.pojo.Movies;
import com.videoadmin.utils.CheckExt;

public class MovieUploadServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public MovieUploadServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		SmartUpload smart = new SmartUpload();
		
		try {
			smart.initialize(this.getServletConfig(), request, response); // 初始化上传操作
			smart.upload(); // 上传准备
			
			// 验证上传文件的后缀名是否符合标准
			if(CheckExt.checkVideoExt(smart.getFiles().getFile(0).getFileExt()) 
						&& CheckExt.checkImageExt(smart.getFiles().getFile(1).getFileExt())){
			
				// 取参数
				String movieName = smart.getRequest().getParameter("movieName");
				String movieYear = smart.getRequest().getParameter("movieYear");
				String movieDirector = smart.getRequest().getParameter("movieDirector");
				String movieActor = smart.getRequest().getParameter("movieActor");
				String movieType = smart.getRequest().getParameter("movieType");
				String movieArea = smart.getRequest().getParameter("movieArea");
				String moviePrice = smart.getRequest().getParameter("moviePrice");
				String movieInfo = smart.getRequest().getParameter("movieInfo");
				
				// 上传操作
				smart.getFiles().getFile(0).saveAs("E:\\OurVideoSystem\\WebRoot\\videos\\" 
						+ smart.getFiles().getFile(0).getFileName());
				smart.getFiles().getFile(1).saveAs("E:\\OurVideoSystem\\WebRoot\\images\\" 
						+ smart.getFiles().getFile(1).getFileName());
				
				// 数据库添加
				Movies movie = new Movies();
				movie.setDirectory("/video/videos/" + smart.getFiles().getFile(0).getFileName());
				movie.setPic_Dir("/video/images/" + smart.getFiles().getFile(1).getFileName());
				movie.setMovieName(movieName);
				movie.setYear(new SimpleDateFormat("yyyy-MM-dd").parse(movieYear));
				movie.setDirector(movieDirector);
				movie.setActor(movieActor);
				movie.setMovieType(movieType);
				movie.setArea(movieArea);
				movie.setCost(Integer.parseInt(moviePrice));
				movie.setInfomation(movieInfo);
				
				DAOFactory.getIMovieDAOInstance().saveOrUpdate(movie);
				request.setAttribute("msg", "上传成功");
			} else{
				request.setAttribute("msg", "上传失败，文件格式不符合要求");
			} 
			
			request.getRequestDispatcher("movie_upload.jsp").forward(request, response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
